import 'package:flutter/cupertino.dart';
import 'package:flutter_application_1/models/other_page.dart';
import 'package:get/get.dart';

class OtherLogic extends GetxController {
  var todos = <Todo>[].obs;
  final inputController = TextEditingController();
  int nextId = 1;

  bool get isAllSelected => todos.isNotEmpty && todos.every((e) => e.done);

  bool get hasAnySelected => todos.any((e) => e.done);

  void toggleAll(bool? value) {
    // value == true → 全选；false → 全不选；null（不定态）时按“反转为全选/全不选”
    final target = value ?? !isAllSelected;
    for (final t in todos) {
      t.done = target;
    }
    todos.refresh();
  }

  void addItem() {
    final text = inputController.text.trim();
    if (text.isNotEmpty) {
      todos.add(Todo(id: nextId++, title: text));
      inputController.clear();
    }
  }

  void delItem(int id) {
    todos.removeWhere((e) => e.id == id);
  }

  // void editItem(int index, String newVal) {
  //   todos[index] = newVal as Todo;
  // }

  toggleDone(int id) {
    final id0 = todos.indexWhere((e) => e.id == id);
    if (id0 == -1) return;
    todos[id0].done = !todos[id0].done;
    todos.refresh();
  }

  @override
  void onClose() {
    inputController.dispose();
    // TODO: implement onClose
    super.onClose();
  }
}
